package com.hk.housekeeping.service.impl;

import com.hk.housekeeping.entity.*;
import com.hk.housekeeping.mapper.ManagerMapper;
import com.hk.housekeeping.service.ManagerServ;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ManagerServImpl implements ManagerServ {
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public ResponseEntity<Integer> login(String username, String password) {
        ResponseEntity<Integer> entity = null;

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            entity = ResponseEntity.error(ResponseCode.MANAGER_EMPTY_ERROR);
        } else {
            int count = managerMapper.selectManager(username, password);
            //查询出的id为null时，因为是int类型所以会映射成0
            if (count != 0) {
                entity = ResponseEntity.success(count);
            } else {
                entity = ResponseEntity.error(ResponseCode.MANAGER_LOGIN_ERROR);
            }
        }

        return entity;
    }

    @Override
    public ResponseEntity<Integer> deleteManager(int mid) {
        ResponseEntity<Integer> entity = null;
        //通过manager_info的mid进行假删除
        int count1 = managerMapper.deleteManagerInfoByMid(mid);
        int count2 = managerMapper.deleteManagerAndInfo(mid);
        if (count1 != 0 && count2 != 0) {
            entity = ResponseEntity.success(1);
        } else {
            entity = ResponseEntity.error(ResponseCode.MANAGER_DELETE_ERROR);
        }
        return entity;
    }

    @Override
    public ResponseEntity<Integer> registerManager(RegisterManager registerManager) {
        ResponseEntity<Integer> entity = null;

        Manager manager = new Manager();
        manager.setUsername(registerManager.getUsername());
        manager.setPassword(registerManager.getPassword());
        int count1 = managerMapper.insertManager(manager);

        ManagerInfo managerInfo = new ManagerInfo();
        managerInfo.setHeadImg(registerManager.getHeadImg());
        managerInfo.setName(registerManager.getName());
        managerInfo.setPhone(registerManager.getPhone());
        managerInfo.setMid(manager.getMid());           //添加到用户密码表后返回的mid
        managerInfo.setRemark(registerManager.getRemark());
        int count2 = managerMapper.insertManagerInfo(managerInfo);

        List<Integer> permissionList = registerManager.getPermission();
        int count3 = 0;     //不授予任何权限，默认为0
        if (permissionList.size() != 0 && permissionList != null) {
            count3 = managerMapper.insertManagerAndPermission(managerInfo.getId(), permissionList);
            if (count3 == 0) {
                System.out.println("权限授予失败！");
                entity = ResponseEntity.error(ResponseCode.MANAGER_PERMISSION_ERROR);
            }
        }

        if (count1 != 0 && count2 != 0) {
            entity = ResponseEntity.success(1);
        } else {
            entity = ResponseEntity.error(ResponseCode.MANAGER_INSERT_ERROR);
        }

         return entity;
    }

    @Override
    public ResponseEntity<Integer> editManager(UpdateManager updateManager) {
        ResponseEntity<Integer> entity = null;

        try {
            //1、首先进行假删除（假删除管理员信息表、登录表、权限表）
            int id = updateManager.getId();
            int count1 = managerMapper.deleteManagerInfo(id);
            int count2 = managerMapper.deleteManagerPermission(id);
            //2、重新插入数据到三个表
            //2.1修改密码
            int count3 = managerMapper.updateManagerPassword(updateManager.getMid(), updateManager.getPassword());
            //2.2插入manager_info表
            ManagerInfo managerInfo = new ManagerInfo();
            managerInfo.setHeadImg(updateManager.getHeadImg());
            managerInfo.setName(updateManager.getName());
            managerInfo.setPhone(updateManager.getPhone());
            managerInfo.setMid(updateManager.getMid());           //添加到用户密码表后返回的mid
            managerInfo.setRemark(updateManager.getRemark());
            int count4 = managerMapper.insertManagerInfo(managerInfo);

            //2.3插入权限表（注意：权限可以不授予，此时权限集合为null，也会引起数据库操作失败，因此权限插入要单独判断）
            List<Integer> permissionList = updateManager.getPermission();
            int count5 = 0;     //不授予任何权限，默认为0
            if (permissionList.size() != 0 && permissionList != null) {
                count5 = managerMapper.insertManagerAndPermission(managerInfo.getId(), permissionList);
                //单独进行操作判断
                if (count5 == 0) {
                    System.out.println("权限授予失败！");
                    entity = ResponseEntity.error(ResponseCode.MANAGER_PERMISSION_ERROR);
                }
            }

            if (count1 != 0 && count2 != 0 && count3 != 0 && count4 != 0) {
                entity = ResponseEntity.success(1);
            } else {
                entity = ResponseEntity.error(ResponseCode.MANAGER_EDIT_ERROR);
            }
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.MANAGER_EDIT_ERROR);
        }

        return entity;
    }

    @Override
    public ResponseEntity<PageUtils<List<ManagerInfo>>> pageSelect(int currentPage, int pageSize) {
        ResponseEntity<PageUtils<List<ManagerInfo>>> entity = null;
        try {
            PageUtils<List<ManagerInfo>> listPageUtils = new PageUtils<>();
            listPageUtils.setCurrentPage(currentPage);
            listPageUtils.setData(managerMapper.selectManagerPage((currentPage - 1) * pageSize, pageSize));
            listPageUtils.setTotalCount(managerMapper.selectTotalCount());

            entity = ResponseEntity.success(listPageUtils);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.MANAGER_PAGE_SELECT_ERROR);
        }
        return entity;
    }

}
